using System;
using System.Collections.Generic;
using System.Text;

namespace WAVMP3Recorder {
  public class RecordModule : Xivo.Shared.IXivoRecorder, IDisposable {
    #region IXivoRecorder Members

    public event Xivo.Shared.Delegates.StringEvent onFilenameChange;

    Xivo.Shared.ChannelInfo _Info = null;
    string _RecordPath;
    string _FileName;
    bool SplitTracks;
    WAVRecorder Recorder;
    DateTime StartTime;

    string RecordingFilename;
    string fullFilename {
      get {
        return System.IO.Path.Combine(_RecordPath, _FileName);
      }
    }

    public string RecordPath {
      get {
        return _RecordPath;
      }
      set {
        _RecordPath = value;
      }
    }

    public Xivo.Shared.ChannelInfo Info {
      get {
        return _Info;
      }
      set {
        if (value != null)
          _Info = value;
        else
          _Info = new Xivo.Shared.ChannelInfo();
        FileName = _Info.getFileName();
      }
    }

    public void StartRecording(Xivo.Shared.ChannelInfo info, bool SplitRecordings) {
      Console.WriteLine("RecordModule.StartRecording");
      if (Recording == false) {
        _Recording = true;
        if (info != null)
          Info = info;
        RecordingFilename = fullFilename;
        Recorder = new WAVRecorder(RecordingFilename);
        StartTime = DateTime.Now;
      }
    }

    public void StopRecording() {
      Console.WriteLine("RecordModule.StopRecording");
      if (Recorder != null) {
        Recorder.StopRecording();
        if (RecordingFilename != "") {
          System.Diagnostics.Process.Start("mp3convert.bat",
            "\"" + RecordingFilename + ".wav\" \"" + RecordingFilename + ".mp3" + "\"");
          RecordingFilename = "";
          if (onFilenameChange != null)
            onFilenameChange(FileName);
        }
      }
      _Recording = false;
    }

    volatile private bool _Recording = false;
    public bool Recording {
      get {
        if (Recorder != null)
          return Recorder.Capturing;
        else
          return false ;
      }
    }

    public TimeSpan RecordTime {
      get { return DateTime.Now - StartTime; }
    }

    public string RecordTimeStr {
      get {
        TimeSpan ts = RecordTime;
        return ts.Hours.ToString("00") + ":" +
          ts.Minutes.ToString("00") + ":" +
          ts.Seconds.ToString("00");
      }
    }

    public string State {
      get {
        if (Recorder != null) {
          if (Recorder.Capturing)
            return "Recording";
          else
            return "Stopped";
        }
        else
          return "-";
      }
    }

    /// <summary>
    /// Filenames should always be passed without extension,
    /// except where stated otherwise. This allows modules to
    /// append the appropriate extension as needed.
    /// </summary>
    public string FileName {
      get {
        if (RecordingFilename != "")
          return RecordingFilename;
        else
          return _FileName;
      }
      set {
        if (_FileName != value) {
          _FileName = value;
          if (onFilenameChange != null)
            onFilenameChange(FileName);
        }
      }
    }

    public System.Windows.Forms.Control getOptionPanel() {
      return new OptionsPanel();
    }

    #endregion

    #region IDisposable Members

    public void Dispose() {
      if (Recorder != null)
        Recorder.StopRecording();
    }

    #endregion
  }
}
